//index.js
//获取应用实例
const app = getApp();
var utils = require('../../utils/util.js');

const ctx = wx.createCanvasContext("bgCanvas"); //创建一个全局的canvas绘图上下文
const ctx2 = wx.createCanvasContext("runCanvas");
let mytime = "";
var n = "";
var w = "";
var h = "";

Page({
  data: {
    support: app.d.support,
    score: 80, //传入的进度， 0~100，绘制到此参数处停止。
    cutover: [
      { name: '考试资讯', img: '../../images/icon/news.png', id: 1, url: '/pages/about/about?typeId=1', type: 'cutover' },
      { name: '专业详情', img: '../../images/icon/major.png', id: 2, url: '/pages/about/about?typeId=2', type: 'cutover' },
      { name: '师资介绍', img: '../../images/icon/teacher.png', id: 3, url: '/pages/about/about?typeId=3', type: 'cutover' },
      { name: '关于我们', img: '../../images/icon/about.png', id: 4, url: '/pages/about/about?typeId=4', type: 'cutover' },
    ],

  },
  run(e) {
    let that = this;
    let src = that.data.src; //每个间隔所需绘制的弧度
    let allSrc = that.data.allSrc; //总共需要绘制的弧度
    n++;
    if (src * n > allSrc) {
      clearInterval(mytime); //如果绘制完成，停掉计时器，绘制结束
      n = 0;
      return;
    }
    // console.log(n);
    let grade = Math.round(src * n / 1.5 * 100); //百分数
    ctx2.arc(w, h, w - 8, 0.75 * Math.PI, (0.75 + src * n) * Math.PI); //每个间隔绘制的弧度
    ctx2.setStrokeStyle("#80E0FE");
    ctx2.setLineWidth("8");
    ctx2.setLineCap("round");
    ctx2.stroke();
    ctx2.beginPath();
    ctx2.setFontSize(20); //注意不要加引号
    ctx2.setFillStyle("#80E0FE");
    ctx2.setTextAlign("center");
    ctx2.setTextBaseline("middle");
    ctx2.fillText(grade + "%", w, h);


    ctx2.draw();
  },

  onLoad: function (e) {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          //url: app.globalData.api_url + 'Homepage/openid',
          url: app.d.path + 'Login/openid',
          data: {
            code: res.code,
          },
          success: function (res) {
            var session_key = res.data.session_key;
            app.d.openid = res.data.openid;
            that.init();
            wx.getSetting({
              success: res => {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                var code = res.code;
                if (res.authSetting['scope.userInfo']) {
                  wx.getUserInfo({
                    success: function (res) {
                      console.log(res.userInfo);

                    }
                  });
                } else {
                  wx.reLaunch({
                    url: '/pages/login/login?code=' + code,
                  })
                }
              }
            })
          }
        })
      },
      fail: function () {
        console.log("登录失败")
      }
    });
    
    //console.log(app.d.openid)
    
    that.setData({
      imgPath:app.d.imgPath
    })
    let allSrc = 0.015 * that.data.score; //应该绘制的弧度
    let src = allSrc / 100 //计算出每个间隔应该绘制多少弧度。 
    that.setData({
      src: src,
      allSrc: allSrc
    });

    clearInterval(mytime);
    mytime = setInterval(that.run, 50)

  },
  
  cutover: function (e) {
    var that = this;
    var url = e.currentTarget.id;
    wx.navigateTo({
      url: url
    })
  },

  init:function(){
    var that = this;
    var sucFunc = function (that, res) {
      //console.log(res.data)
      that.setData({
        banner: res.data.banner,
        datas: res.data.datas,
        video:res.data.video
      })
    }
    var data = {openid:app.d.openid}
    utils.wxRequest(that, app.d.path, "Index/index", data, sucFunc);
  },

  onShareAppMessage: function (res) {
    return {
      title: app.d.shareTxt,
      path: '',
      success: function (res) {
        //console.log("转发成功");
      },
      fail: function (res) {
        //console.log("转发失败")
      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {
  //   this.init();
  //   setTimeout(function () {
  //     wx.stopPullDownRefresh();
  //   }, 1500)
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.createSelectorQuery().select('#canvas-one').boundingClientRect(function (rect) {//监听canvas的宽高
      //console.log(rect);
      w = parseInt(rect.width / 2); //获取canvas宽的的一半
      h = parseInt(rect.height / 2); //获取canvas高的一半，
      //获取宽高的一半是为了便于找到中心点

      ctx.arc(w, h, w - 8, 0.75 * Math.PI, 2.25 * Math.PI); //绘制圆形弧线
      ctx.setStrokeStyle("#dddddd"); //设置填充线条颜色
      ctx.setLineWidth("8");     //设置线条宽度
      ctx.setLineCap("round");        //设置线条端点样式
      ctx.stroke();     //对路径进行描边，也就是绘制线条。

      ctx.draw();       //开始绘制
    }).exec()



  },

  showImg: function (e) {
    var that = this;
    var url = '/pages/about/about?typeId=5&id=' + e.currentTarget.dataset.id;
    wx.navigateTo({
      url: url
    })
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.init();
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },


  //加载更多
  onReachBottom: function () {
    console.log('加载更多')
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
        recommends: [
          {
            goodId: 7,
            name: 'OLAY玉兰油精油沐浴露玫瑰滋养二合一450ml',
            url: 'bill',
            imageurl: 'http://mz.djmall.xmisp.cn/files/product/20161213/148162245074.jpg',
            newprice: "￥36.60",
            oldprice: "￥40.00",
          },
          {
            goodId: 10,
            name: '兰蔻玫瑰清滢保湿柔肤水嫩肤水化妆水400ml补水保湿温和不刺激',
            url: 'bill',
            imageurl: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057937593.jpg',
            newprice: "￥30.00",
            oldprice: "￥80.00",
          }, {
            goodId: 11,
            name: 'Lancome/兰蔻清莹柔肤爽肤水/粉水400ml补水保湿玫瑰水化妆水',
            url: 'bill',
            imageurl: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg',
            newprice: "￥30.00",
            oldprice: "￥80.00",
          },
          {
            goodId: 12,
            name: '美国CLINIQUE倩碧黄油无油/特效润肤露125ml',
            url: 'bill',
            imageurl: 'http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg',
            newprice: "￥239.00",
            oldprice: "￥320.00",
          },
          {
            goodId: 13,
            name: '法国LANCOME兰蔻柔皙轻透隔离防晒乳霜50ML2017年3月到期',
            url: 'bill',
            imageurl: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058014894.jpg',
            newprice: "￥130.00",
            oldprice: "￥180.00",
          },
        ],
      })
    }, 1000)
  }


// wx.login({
//     success: res => {
//       // 发送 res.code 到后台换取 openId, sessionKey, unionId
//       console.log(res.code)
//       var that = this
//       wx.request({
//         //url: app.globalData.api_url + 'Homepage/openid',
//         url: that.d.path + 'Login/openid',
//         data: {
//           code: res.code,
//         },
//         success: function (res) {
//           console.log(res);
//           var session_key = res.data.session_key;

//           wx.getSetting({
//             success: res => {
//               // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
//               wx.getUserInfo({
//                 success: res => {
//                   // 可以将 res 发送给后台解码出 unionId
//                   //console.log(res)
//                   wx.request({
//                     url: that.d.path + 'Login/decrypt',
//                     data: {
//                       session_key: session_key,
//                       encryptedData: res.encryptedData,
//                       iv: res.iv
//                     },
//                     success: function (res) {
//                       console.log(res);
//                       app.d.openid = res.data.openId;

//                       wx.request({
//                         url: that.d.path + 'Login/user',
//                         data: {
//                           openid: app.d.openid,
//                           img: res.data.avatarUrl,
//                           name: res.data.nickName
//                         },
//                         success: function (ress) {
//                           //console.log(res);

//                         }
//                       })
//                     }
//                   })
//                   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
//                   // 所以此处加入 callback 以防止这种情况
//                   if (this.userInfoReadyCallback) {
//                     this.userInfoReadyCallback(res)
//                   }
//                 }
//               })
//             }

//           })
//         }
//       })
//     },
//     fail: function () {
//       console.log("登录失败")
//     }
//   })



})
